package com.dyoud.api.user.controller;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.dyoud.api.ReturnInfo;
import com.dyoud.api.shop.service.EmployeeApiService;
import com.dyoud.api.shop.service.ShopApiService;
import com.dyoud.api.shop.shopInfo.dto.UpdateShopInfoDto;
import com.dyoud.api.user.dto.AddUserIncomeDto;
import com.dyoud.api.user.dto.AppQueryUserIncomeDto;
import com.dyoud.api.user.service.UserIncomeApiService;
import com.dyoud.core.ResponseBean;
import com.dyoud.shop.api.model.ShopInfo;

@RestController
@RequestMapping("/userIncome")
public class UserIncomeController {

    @Resource
    private UserIncomeApiService userIncomeApiService;

    @Resource
    private EmployeeApiService employeeApiService;

    @Resource
    private ShopApiService shopApiService;
    /***
     * 用户添加收益记录
     * 
     * @param dto
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseBean addUserIncome(@RequestBody @Validated AddUserIncomeDto dto) {
        boolean isOk = userIncomeApiService.addUserIncome(dto);
        if (!isOk) {
            return new ResponseBean().fail(ReturnInfo.ADD_USERINCOME_CODE, ReturnInfo.ADD_USERINCOME_MESSAGE);
        }

        return new ResponseBean().success();
    }

    /***
     * 根据商户id查询用户收益记录,倒序根据创建时间查询
     * 
     * @param shopId
     * @return
     */
    @RequestMapping(value = "/{shopId}/userIncomeRecord", method=RequestMethod.GET)
    public ResponseBean selectByShopIdOrderByCreateTime(@PathVariable("shopId") Long shopId,
            @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            AppQueryUserIncomeDto appQueryUserIncomeDto) {
        ShopInfo shopInfo = shopApiService.getByShopId(shopId);
        
        //先默认查询已经收益的用户记录
        appQueryUserIncomeDto.setState(1);
        
        appQueryUserIncomeDto.setShopId(shopId);
        if(shopInfo == null) {
            return new ResponseBean().fail(ReturnInfo.SHOP_NULL_CODE, ReturnInfo.SHOP_NULL_MESSAGE);
        }
        return new ResponseBean().success(userIncomeApiService.selectByShopIdOrderByCreateTime(pageNo, pageSize, appQueryUserIncomeDto));
    }
    /***
     * 根据商户id查询所有用户汇总消费记录，根据userId汇总
     * 
     * @param shopId
     * @return
     */
    @RequestMapping(value = "/{shopId}/allUsers", method=RequestMethod.GET)
    public ResponseBean selectByShopIdGroupByUserId(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,@PathVariable("shopId") Long shopId) {
        ShopInfo shopInfo = shopApiService.getByShopId(shopId);
        if(shopInfo == null) {
            return new ResponseBean().fail(ReturnInfo.SHOP_NULL_CODE, ReturnInfo.SHOP_NULL_MESSAGE);
        }
        return new ResponseBean().success(userIncomeApiService.selectByShopIdGroupByUserId(pageNo, pageSize, shopId));
    }
    

}
